<template>
  <div>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="企业名称查询"
          :clearable="true"
          @keyup.enter.native="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.lender_idcard"
          placeholder="身份证号码查询"
          :clearable="true"
          @keyup.enter.native="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="经营者名称查询"
          :clearable="true"
          @keyup.enter.native="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-download"
          type="primary"
          :loading="exportLoading"
          @click="exportExcel()"
        >
          导出
        </el-button>
      </el-form-item>
    </el-form>

    <c-table border class="table-sm" v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <div class="text-truncate" :title="scope.row.address">{{ scope.row.address }}</div>
      </template>
      <template v-slot:cooperate="scope">
        <div>
          <span v-if="+scope.row.is_approval !== 1">暂无合作</span>
        </div>
        <div v-if="+scope.row.is_approval === 1">{{ scope.row.service_name }}</div>
      </template>
      <template v-slot:itemStatus="scope">
        <span class="badge" :class="`badge-${scope.row.itemStatusStyle}`">
          {{ scope.row.itemStatusLabel }}
        </span>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="viewVisible"
      :close-on-click-modal="false"
      append-to-body
      top="30px"
      width="80%"
    >
      <el-tabs>
        <el-tab-pane label="项目信息">
          <info-view :related-data="viewData"></info-view>
        </el-tab-pane>
        <el-tab-pane label="担保意向函" name="letter">
          <letter-view :query-id="viewId"></letter-view>
        </el-tab-pane>
        <el-tab-pane label="放款通知书">
          <loan-notice :query-id="viewId"></loan-notice>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>

    <attach-cmpt :visible.sync="attachVisible" :customer-related-id="attachRelatedId"></attach-cmpt>

    <register-edit-dialog
      :visible.sync="editVisible"
      :related-data="editRow"
      type="2"
      @success="handleSuccess"
    ></register-edit-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

import { paramsMixins } from '@/bank/views/business/28/normal/mixins'
import { exportTable } from '@/bank/plugins/export'

import SelectAddress from '@/components/select/SelectAddress'
import AttachCmpt from '@/credit/views/city/program/Attach'
import RegisterEditDialog from '@/bank/views/mission/RegisterEditDialog'
import InfoView from '@/credit/views/city/bus/normal/Info'
import LoanNotice from '@/credit/views/county/unionProvince/components/LoanNotice'
import LetterView from '@/credit/views/county/unionProvince/components/LetterView'

export default {
  components: {
    SelectAddress,
    LoanNotice,
    AttachCmpt,
    RegisterEditDialog,
    InfoView,
    LetterView,
  },
  mixins: [paramsMixins],
  props: {},
  data() {
    return {
      loading: false,
      exportLoading: false,

      order: {
        load_date: 'desc',
      },
      params: {
        index: 1,
        size: 10,
        'contract_approval_status.gte': 1,
      },
      tableName: 'customer_related',
      tableData: [],
      tableCount: 0,
      tableColumns: [
        {
          attrs: { label: '项目名称', prop: 'customer_name', minWidth: '120px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '受理类型', prop: 'project_type', align: 'center' } },
        { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
        { attrs: { label: '负责人', prop: 'corp_name', align: 'center' } },
        { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },
        { attrs: { label: '拟放款金额', prop: 'reply_funds', align: 'center' } },
        { attrs: { label: '合作担保', prop: 'commpany', align: 'center' }, slot: 'cooperate' },
        { attrs: { label: '受理银行', prop: 'bank_name', align: 'center' } },
        {
          attrs: { label: '放款登记状态', prop: 'itemStatusLabel', align: 'center' },
          slot: 'itemStatus',
        },
        {
          attrs: { label: '操作', align: 'center', minWidth: '100' },
          operations: [
            {
              content: '放款登记',
              icon: 'fa fa-gavel',
              event: this.handleEdit,
              ifRender: row => row.editable,
            },
            // { content: '上传附件', icon: 'fa fa-clipboard', event: this.hanleAttach },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],

      viewVisible: false,
      viewId: '',
      viewData: {},

      attachVisible: false,
      attachRelatedId: '',

      editVisible: false,
      editRow: {},
    }
  },
  computed: {
    ...mapGetters(['org', 'user']),
  },
  watch: {
    status: {
      handler: function (val) {
        if (+val === 1) {
          this.params['loan_approval_status.eq'] = 2
          this.params['loan_approval_status.gte'] = ''
        } else {
          this.params['loan_approval_status.eq'] = ''
          this.params['loan_approval_status.gte'] = 2
        }
        this.getData()
      },
      immediate: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    getParams() {
      return {
        ...this.params,
        ...this.baseParams,
      }
    },
    fetchData(params) {
      return this.$utils.promise(
        this.$api.getData({
          table: this.tableName,
          params,
          order: this.order || {},
        })
      )
    },
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      const [res = {}] = await this.fetchData(this.getParams())
      this.loading = false

      const data = res.data || {}
      this.tableCount = data.totalCount || 0
      this.tableData = (data.data || []).map(this.dataAdapter)
    },
    dataAdapter(item) {
      const enumStatusLabel = {
        0: '待审批',
        1: '审批中',
        2: '待放款',
        3: '已放款',
        4: '已登记',
      }
      const enumStatusStyle = {
        0: 'info',
        1: 'warning',
        2: 'warning',
        3: 'success',
        4: 'success',
      }

      if (this.org.orgCode.includes(item.bank_code) && +item.submit_type === 5) {
        item.project_type = '自建项目'
      } else if (this.clientId && this.clientId.includes(item.public_bank_ids)) {
        item.project_type = '推荐项目'
      } else {
        item.project_type = '受理项目'
      }

      item.load_date = this.$utils.dateFormat(item.load_date, 'yyyy-MM-dd')
      item.commpany = +item.is_approval === 1 ? `已合作： ${item.service_name}` : '暂无合作'
      item.itemStatusLabel = enumStatusLabel[item.loan_approval_status] || ''
      item.itemStatusStyle = enumStatusStyle[item.loan_approval_status] || ''
      item.editable = +item.loan_approval_status < 3
      return item
    },
    exportExcel() {
      this.exportLoading = true
      exportTable({
        columns: this.tableColumns,
        table: this.tableName,
        params: this.getParams(),
        order: this.order,
        adapter: this.dataAdapter,
        filename: '放款登记',
      }).finally(() => {
        this.exportLoading = false
      })
    },
    handleView(row) {
      this.viewId = row.customer_related_id
      this.viewVisible = true
      this.viewData = row
    },
    hanleAttach(row) {
      this.attachVisible = true
      this.attachRelatedId = row.customer_related_id
    },

    handleEdit(row) {
      this.editRow = { ...row }
      this.editVisible = true
    },
    handleSuccess() {
      this.editVisible = false
      this.getData(this.params.index)
      this.$store.dispatch('getTaskCountNormal', this.productId)
    },
  },
}
</script>
<style lang="scss" scoped></style>
